capture program drop policy_sim_strength
program policy_sim_strength
	version 16

	set varabbrev off

	//the idea is to see what fraction of all possible combination of variables are banned by each bank
    // and use this as a crude measure of the policy strength

	local fulllist isNew isExpansion isMiningProj isPowerProj isMountaintopProj isMountaintopComp isHighCarbonProj isNewCustomer ///
		           isProjFin isThermal hasNoDecarbonStrat isMiningCo isPowerCo isCorpFin CoalFracRevParent CoalSharePowerParent

	clear
    tempfile crossed
    clear 
    set obs 22
    gen year = 2008+_n
    save `crossed'

    foreach item in `fulllist' {
    	clear 
    	set obs 2
    	gen `item' = _n == 1
    	cross using `crossed'
    	save `crossed', replace
    }
    count if year == 2009 
    local scenario_count = r(N)

    gen CountryParent = ""
    gen CountryGroup = ""
    gen ContinentParent = ""


    //drop impossible scenarios:
    drop if isMiningCo == 0 & isMountaintopComp == 1
    drop if isMiningProj == 0 & isMountaintopProj == 1
    drop if isMiningProj == 1 & isPowerProj == 1
    drop if isNew == 1 & isExpansion == 1
    drop if isCorpFin == 1 & isProjFin == 1
    drop if isPowerProj == 1 & isThermal == 0
    drop if isPowerCo == 1 & isThermal == 0

    //run the simulation
    include "../Data_Cleaning/Policy_Coding_Function.do"

    //count the number of banned scenarios by year
    gcollapse (sum) ban_*, by(year)

    greshape long ban_, i(year) j(BankID)

    rename ban_ banned_scenario_count
    gen ban_intensity = banned_scenario_count/`scenario_count'

    sort BankID year
    order BankID year

end


capture program drop policy_sim_univariate

program policy_sim_univariate
    version 16
    syntax name

    set varabbrev off
    //display "`namelist'"

    //the idea is to see if there is a policy that in SOME way bans a certain variable
    // to do this, we simulate all combinations of indicator variables except the one 
    // in question. and then see if any of these are sensitive to changing the variable
    // that is in question.

    local fulllist isNew isExpansion isMiningProj isPowerProj isMountaintopProj isMountaintopComp isHighCarbonProj isNewCustomer ///
                   isProjFin isThermal hasNoDecarbonStrat isMiningCo isPowerCo isCorpFin CoalFracRevParent CoalSharePowerParent

    local allelse: list fulllist-namelist

    clear
    tempfile crossed
    clear 
    set obs 22
    gen year = 2008+_n
    save `crossed'

    //make 2^n observations of all possible combinations of above binary variables
    foreach item in `allelse' {
        clear 
        set obs 2
        gen `item' = _n == 1
        cross using `crossed'
        save `crossed', replace
    }

    gen CountryParent = ""
    gen CountryGroup = ""
    gen ContinentParent = ""



    //try these for the variable in question = 1 
    gen `namelist' = 0

    //drop impossible scenarios:
    drop if isMiningCo == 0 & isMountaintopComp == 1
    drop if isMiningProj == 0 & isMountaintopProj == 1
    drop if isMiningProj == 1 & isPowerProj == 1
    drop if isNew == 1 & isExpansion == 1
    drop if isCorpFin == 1 & isProjFin == 1
    drop if isPowerProj == 1 & isThermal == 0
    drop if isPowerCo == 1 & isThermal == 0


    include "../Data_Cleaning/Policy_Coding_Function.do"
    rename ban_* v0_ban_*

    replace `namelist' = 1

    //drop impossible scenarios (incase flipping namelist from 0 to 1 created a new impossible scenario)
    drop if isMiningCo == 0 & isMountaintopComp == 1
    drop if isMiningProj == 0 & isMountaintopProj == 1
    drop if isMiningProj == 1 & isPowerProj == 1
    drop if isNew == 1 & isExpansion == 1
    drop if isCorpFin == 1 & isProjFin == 1
    drop if isPowerProj == 1 & isThermal == 0
    drop if isPowerCo == 1 & isThermal == 0



    include "../Data_Cleaning/Policy_Coding_Function"
    rename ban_* v1_ban_*

    gen x = _n

    keep x year *_ban_*

    greshape long v0_ban_ v1_ban_, i(x year) j(BankID)

    replace v0_ban_ = 0 if missing(v0_ban_)
    replace v1_ban_ = 0 if missing(v1_ban_)
    gen vdiff = v1_ban_ - v0_ban_
    gcollapse (mean) vdiff, by(BankID year)

end



capture program drop policy_sim_strength_min
program policy_sim_strength_min
	version 16

	set varabbrev off

	//the idea is to see what fraction of all possible combination of variables are banned by each bank
    // and use this as a crude measure of the policy strength

	local fulllist isNew isExpansion isMiningProj isPowerProj isMountaintopProj isMountaintopComp isHighCarbonProj isNewCustomer ///
		           isProjFin isThermal hasNoDecarbonStrat isMiningCo isPowerCo isCorpFin CoalFracRevParent CoalSharePowerParent

	clear
    tempfile crossed
    clear 
    set obs 22
    gen year = 2008+_n
    save `crossed'

    foreach item in `fulllist' {
    	clear 
    	set obs 2
    	gen `item' = _n == 1
    	cross using `crossed'
    	save `crossed', replace
    }
    count if year == 2009 
    local scenario_count = r(N)

    gen CountryParent = ""
    gen CountryGroup = ""
    gen ContinentParent = ""


    //drop impossible scenarios:
    drop if isMiningCo == 0 & isMountaintopComp == 1
    drop if isMiningProj == 0 & isMountaintopProj == 1
    drop if isMiningProj == 1 & isPowerProj == 1
    drop if isNew == 1 & isExpansion == 1
    drop if isCorpFin == 1 & isProjFin == 1
    drop if isPowerProj == 1 & isThermal == 0
    drop if isPowerCo == 1 & isThermal == 0

    //run the simulation
    include "../Data_Cleaning/Policy_Coding_Function_Min.do"

    //count the number of banned scenarios by year
    gcollapse (sum) ban_*, by(year)

    greshape long ban_, i(year) j(BankID)

    rename ban_ banned_scenario_count
    gen ban_intensity = banned_scenario_count/`scenario_count'

    sort BankID year
    order BankID year

end




capture program drop policy_sim_univariate_min
program policy_sim_univariate_min
    version 16
    syntax name

    set varabbrev off
    //display "`namelist'"

    //the idea is to see if there is a policy that in SOME way bans a certain variable
    // to do this, we simulate all combinations of indicator variables except the one 
    // in question. and then see if any of these are sensitive to changing the variable
    // that is in question.

    local fulllist isNew isExpansion isMiningProj isPowerProj isMountaintopProj isMountaintopComp isHighCarbonProj isNewCustomer ///
                   isProjFin isThermal hasNoDecarbonStrat isMiningCo isPowerCo isCorpFin CoalFracRevParent CoalSharePowerParent

    local allelse: list fulllist-namelist

    clear
    tempfile crossed
    clear 
    set obs 22
    gen year = 2008+_n
    save `crossed'

    //make 2^n observations of all possible combinations of above binary variables
    foreach item in `allelse' {
        clear 
        set obs 2
        gen `item' = _n == 1
        cross using `crossed'
        save `crossed', replace
    }

    gen CountryParent = ""
    gen CountryGroup = ""
    gen ContinentParent = ""



    //try these for the variable in question = 1 
    gen `namelist' = 0

    //drop impossible scenarios:
    drop if isMiningCo == 0 & isMountaintopComp == 1
    drop if isMiningProj == 0 & isMountaintopProj == 1
    drop if isMiningProj == 1 & isPowerProj == 1
    drop if isNew == 1 & isExpansion == 1
    drop if isCorpFin == 1 & isProjFin == 1
    drop if isPowerProj == 1 & isThermal == 0
    drop if isPowerCo == 1 & isThermal == 0


    include "../Data_Cleaning/Policy_Coding_Function_Min.do"
    rename ban_* v0_ban_*

    replace `namelist' = 1

    //drop impossible scenarios (incase flipping namelist from 0 to 1 created a new impossible scenario)
    drop if isMiningCo == 0 & isMountaintopComp == 1
    drop if isMiningProj == 0 & isMountaintopProj == 1
    drop if isMiningProj == 1 & isPowerProj == 1
    drop if isNew == 1 & isExpansion == 1
    drop if isCorpFin == 1 & isProjFin == 1
    drop if isPowerProj == 1 & isThermal == 0
    drop if isPowerCo == 1 & isThermal == 0



    include "../Data_Cleaning/Policy_Coding_Function_Min.do"
    rename ban_* v1_ban_*

    gen x = _n

    keep x year *_ban_*

    greshape long v0_ban_ v1_ban_, i(x year) j(BankID)

    replace v0_ban_ = 0 if missing(v0_ban_)
    replace v1_ban_ = 0 if missing(v1_ban_)
    gen vdiff = v1_ban_ - v0_ban_
    gcollapse (mean) vdiff, by(BankID year)

end
